<template>
	<div>
		<el-row>
			<el-col :span="24">

				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item label="学生名称">
						<el-input size="small" v-model="formInline.name" placeholder="请输入学生名称"></el-input>
					</el-form-item>
					<el-form-item label="学号">
						<el-input size="small" v-model="formInline.xueHao" placeholder="请输入学号"></el-input>
					</el-form-item>

					<el-form-item>
						<el-button size="small" type="primary" @click="onSubmit">查询</el-button>
					</el-form-item>
				</el-form>


			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24">
				<el-button type="primary" icon="el-icon-plus" size="mini" plain
					@click="dialogFormVisible=true">新增</el-button>
				<el-button type="danger" icon="el-icon-search">搜索</el-button>
				<el-button type="info" icon="el-icon-search">搜索</el-button>

			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24">
				<!-- 
			   1. :data 数据 对象数组 [{}]
			   2.  el-table-column   prop 数据项的名称  label 列名
			   -->
				<el-table :data="tableData" style="width: 100%" @sort-change="sortData">
					<el-table-column prop="id" label="序号" width="180" sortable="custom">
					</el-table-column>
					<el-table-column prop="name" label="学生姓名" width="180">
					</el-table-column>
					<el-table-column prop="xueHao" label="学生学号" width="180">
					</el-table-column>
					<el-table-column prop="password" label="密码" width="180">
					</el-table-column>
					<el-table-column prop="phone" label="联系电话" width="180">
					</el-table-column>
					<el-table-column prop="email" label="联系邮箱" width="180">
					</el-table-column>
					<el-table-column prop="school" label="所在学校" width="180">
					</el-table-column>
					<el-table-column prop="major" label="所学专业" width="180">
					</el-table-column>
					<el-table-column prop="avatar" label="头像" width="180">
						<template slot-scope="a">
							 <el-image
							      style="width: 100px; height: 100px"
							      :src="'http://localhost:8080'+a.row.avatar"
								  :preview-src-list="['http://localhost:8080'+a.row.avatar]"
							      :fit="fit"></el-image>
						</template>
						
					</el-table-column>
					<el-table-column prop="createTime" label="创建时间" width="180">
					</el-table-column>
					<el-table-column prop="updateTime" label="更新时间" width="180">
					</el-table-column>
					



					<el-table-column label="操作" width="180">

						<template slot-scope="a">
							<el-button type="primary" icon="el-icon-edit" circle @click="edit(a.row)"></el-button>


							<el-popconfirm @confirm="del(a.row.id)" title="这是一段内容确定删除吗？">
								<!-- <el-button slot="reference">删除</el-button> -->
								<el-button type="danger" icon="el-icon-delete" circle slot="reference"></el-button>
							</el-popconfirm>
						</template>
					</el-table-column>
				</el-table>

			</el-col>
		</el-row>

		<el-row>
			<el-col :span="24" style="display: flex;justify-content: end;">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
					:current-page="formInline.pageNo" :page-sizes="[2,10, 20, 30, 40]" :page-size="formInline.pageSize"
					layout="total, sizes, prev, pager, next, jumper" :total="total">
				</el-pagination>
			</el-col>
		</el-row>


		<el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
			<el-form :model="form">
				<el-form-item label="学生名称" :label-width="formLabelWidth">
					<el-input v-model="form.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="学生学号" :label-width="formLabelWidth">
					<el-input v-model="form.xueHao" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="学生密码" :label-width="formLabelWidth">
					<el-input v-model="form.password" autocomplete="off" show-password></el-input>
				</el-form-item>
				<el-form-item label="联系电话" :label-width="formLabelWidth">
					<el-input v-model="form.phone" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="联系邮箱" :label-width="formLabelWidth">
					<el-input v-model="form.email" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="所在学校" :label-width="formLabelWidth">
					<el-input v-model="form.school" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="所学专业" :label-width="formLabelWidth">
					<el-input v-model="form.major" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="头像" :label-width="formLabelWidth">
					<el-upload class="avatar-uploader" 
					action="/api/upload"
						:show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
						<img v-if="form.avatar" :src="'http://localhost:8080/'+form.avatar" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="add">确 定</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	import axios from 'axios'

	import {
		getStudents,
		addStudents,
		getOne,
		delStudents,
		editStudents	
	} from '../api/Student.js'

	export default {
		data() {
			return {
				formInline: {
					name: '',
				    xueHao:'',
					pageNo: 1,
					pageSize: 10,
					sort: "id",
					order: "desc"
				},
				total: 0,
				tableData: [],
				dialogFormVisible: false,
				form: {
					name: '',
					xueHao:'',
					password: '',
					phone:'',
					email:'',
					school:'',
					major:'',
					avatar:''
				},
				formLabelWidth: '120px',
				dialogTitle: "新增学员",
				imageUrl: ''
			}
		},
		methods: {
			
			handleAvatarSuccess(res, file) {
				// code
				if(res.code==200){
					this.form.avatar = res.data.url;
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'images/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;

				// if (!isJPG) {
				// 	this.$message.error('上传头像图片只能是 JPG 格式!');
				// }
				if (!isLt2M) {
					this.$message.error('上传头像图片大小不能超过 2MB!');
				}
				return  isLt2M;
			},
			onSubmit() {
				this.getData();
			},
			del(id) {

				delStudents({
					id
				}).then(res => {
					if (res.data.code == 200) {
						this.$message({
							message: "删除成功",
							type: "success"
						})
					} else {
						this.$message({
							message: res.data.message,
							type: "error"
						})

					}
				})
				this.getData();
			},
			add() {

				if (this.form.id > 0) {
					// 新增
					editStudents(this.form).then(res => {
						if (res.data.code == 200) {
							this.$message({
								message: "编辑成功",
								type: "success"
							})
						} else {
							this.$message({
								message: res.data.message,
								type: "error"
							})

						}
						this.dialogFormVisible = false;

						this.getData();
					})
				} else {
					// 新增
					addStudents(this.form).then(res => {
						if (res.data.code == 200) {
							this.$message({
								message: "新增成功",
								type: "success"
							})
						} else {
							this.$message({
								message: res.data.message,
								type: "error"
							})

						}
						this.dialogFormVisible = false;

						this.getData();
					})
				}


			},
			sortData(res) {
				console.log(res)
				if (res.order == "descending") {
					this.formInline.order = "desc";
				} else {
					this.formInline.order = "asc";
				}
				this.formInline.sort = res.prop;
				this.getData();
			},
			handleCurrentChange(num) {
				this.formInline.pageNo = num;

				this.getData();
			},
			handleSizeChange(size) {
				this.formInline.pageSize = size;

				this.getData();
			},
			edit(student) {
				this.dialogFormVisible = true;
				this.dialogTitle = "编辑学员信息";

				// this.form.name =user.username;
				// this.form.region =user.address;

				//发送请求
				getOne(student.id).then(res => {
					console.log(res.data)
					this.form = res.data;
				})
			},
			getData() {
				getStudents(this.formInline).then((res) => {
					this.tableData = res.data.data.rows;
					this.total = res.data.data.total;
				})
			}
		},
		mounted() {
			// axios.get("/api/user/list").then((res)=>{
			// 	let data = res.data.data.rows;
			// 	console.log(data)
			// 	this.tableData = data;
			// })

			// axios.get("/api/user/list").then((res)=>{
			// 	this.tableData  = res.data.data.rows;
			// })
			this.getData()

		}
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>